package cn.b.sky.flowable.service.impl;

import cn.b.sky.flowable.constans.GlobalDefine;
import cn.b.sky.flowable.dao.DeploymentNodeDao;
import cn.b.sky.flowable.dao.FlowNodeDao;
import cn.b.sky.flowable.dao.FlowNodeEventDao;
import cn.b.sky.flowable.dao.FlowNodeUserDao;
import cn.b.sky.flowable.model.DeploymentNode;
import cn.b.sky.flowable.model.FlowNode;
import cn.b.sky.flowable.model.FlowNodeEvent;
import cn.b.sky.flowable.model.FlowNodeUser;
import cn.b.sky.flowable.service.DeploymentNodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("deploymentNodeService")
public class DeploymentNodeServiceImpl implements DeploymentNodeService {

  @Autowired
  private FlowNodeDao flowNodeDao;

  @Autowired
  private DeploymentNodeDao deploymentNodeDao;

  @Autowired
  private FlowNodeUserDao flowNodeUserDao;

  @Autowired
  private FlowNodeEventDao flowNodeEventDao;

  @Override
  public List<FlowNodeUser> getFlowNodeUserByDeploymentNodeId(Long deploymentNodeId) {

    return flowNodeUserDao.selectFlowNodeUserByDeployNodeId(deploymentNodeId);
  }

  @Override
  public List<FlowNodeEvent> getFlowNodeEventByDeploymentNodeId(Long deploymentNodeId) {
    //  Auto-generated method stub
    return null;
  }

  @Override
  public DeploymentNode getFlowNodeByDeploymentIdAndActivitId(String deploymentId, String activitId, String tenantId) {

    return deploymentNodeDao.selectDeploymentNodeByDeployIdAndCode(deploymentId, tenantId, activitId);
  }

  @Override
  public void saveDeploymentNode(DeploymentNode deploymentNode) {
    //  Auto-generated method stub

  }

  @Override
  public void saveFlowNodeUser(FlowNodeUser flowNodeUser) {
    //  Auto-generated method stub

  }

  @Override
  public void saveFlowNodeEvent(FlowNodeEvent flowNodeEvent) {
    //  Auto-generated method stub

  }

  @Override
  public void copyModelNodeToDeploymentNode(String modelId, String tenantId, String deploymentId) {
    if (StringUtils.isBlank(deploymentId)) {
      return;
    }
    List<FlowNode> flowNodes = flowNodeDao.selectFlowNodeByModelIdAndTenantId(modelId, tenantId);
    if (flowNodes != null) {
      for (FlowNode flowNode : flowNodes) {
        Long flowNodeId = flowNode.getId();
        flowNode.setDeploymentId(deploymentId);
        deploymentNodeDao.saveDeploymentNode(flowNode);
        Long deployMentNodeId = flowNode.getId();
        List<FlowNodeUser> users = flowNodeUserDao.selectFlowNodeUserByFlowNodeId(flowNodeId);
        for (FlowNodeUser user : users) {
          user.setDeployMentNodeId(deployMentNodeId);
          user.setFlowNodeId(null);
          flowNodeUserDao.insertFlowNodeUser(user);
        }
        List<FlowNodeEvent> events = flowNodeEventDao.selectFlowNodeEventByFlowNodeId(flowNodeId);
        for (FlowNodeEvent event : events) {
          event.setDeployMentNodeId(deployMentNodeId);
          event.setFlowNodeId(null);
          flowNodeEventDao.insertFlowNodeEvent(event);
        }
      }
    }
  }

  @Override
  public String getStartEventFormKey(String deploymentId, String tenantId) {
    if(StringUtils.isBlank(deploymentId) || StringUtils.isBlank(tenantId)) {
      return null;
    } 
    return deploymentNodeDao.selectStartFormKeyByDeploymentIdAndTenantId(deploymentId, tenantId, GlobalDefine.WorkFlow.STARTEVENT);
  }

}
